#include "project.h"

void  Start_Ui()//老师登录界面
{
    printf("----------------------------------------\n");
    printf("---------**学生管理系统**----------------\n");
    printf("----------------------------------------\n");

    
    return ;
}


char *Hide_Passwd()
{
    system("stty -icanon");
    system("stty -echo");

     char* passwd = (char*)malloc(sizeof(STR_DATA_MAX_LEN));
     if (passwd == (char*)NULL)
     {
        perror("malloc ...");
        return (char*)NULL;
     }
     
    printf("请输入管理员密码:");

    for (int i = 0; i < STR_DATA_MAX_LEN; i++)
    {
        passwd[i] = getchar();
        if (passwd[i] == '\n')
        {
            passwd[i] = '\0';
            break;
        }
        else if (passwd[i] == DEL_NUM && i == 0)
        {
            passwd[i] = '\0';
            i = -1;

            continue;
        }
        else if (passwd[i] == DEL_NUM && i != 0)
        {
            printf("\b \b");
            passwd[i]='\0';
            i-=2;

            continue;
        }
        else
        {
            printf("*");
        }
    }
        system("stty echo");

    return passwd;
}


bool Login(TEA_p teacher)
{
    char name[STR_DATA_MAX_LEN] = {0};
    

     printf("请输入管理员名字:");
    scanf("%s",name); 
    
    getchar();
    char*passwd =Hide_Passwd();

    if (strcmp(name,teacher->teacher_name) == 0 &&
        strcmp(passwd,teacher->teacher_passwd) == 0 )
    {
       printf("\n登陆成功!\n");
       return true;
    }
    else
        printf("用户名或密码错误,");

    return false;
}


bool Function_Select(TEA_p teacher)
{
    if(teacher == (TEA_p)NULL)
    {
        printf("数据库异常！\n");
        return false;
    }
    int choice;
    sleep(2);
    system("clear");
    while (1)
    {
        printf("\n====== 功能菜单 ======\n");
        printf("1. 添加学生\n");
        printf("2. 删除学生\n");
        printf("3. 修改信息\n");
        printf("4. 查找学生\n");
        printf("5. 显示所有\n");
        printf("6. 成绩排序\n");
        printf("7. 退出系统\n");
        printf("请选择操作: ");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:if(Add_Student_Node(teacher)==false)
           {
            printf("添加学生失败\n");
            goto error;
           }
           break;
        case 2:if (Delete_Student(teacher)==false)
            {
                printf("删除学生失败\n");
                goto error;
            }
            break;
        case 3:if (Modify_Student(teacher)==false)
            {
                printf("修改学生信息失败!\n");
                goto error;
            }
            break;
        case 4: Search_Student(teacher);break;
        case 5:if(Show_All_Student(teacher)==false)
            {
             printf("展示学生失败\n");
             goto error;
            }
             break; 
        case 6:if (Sort_Score(teacher)== false)
            {
                printf("排序失败!\n");
                goto error;
            }
            break;
        case 7:Free_System(teacher);
        goto error;
        default:printf("无效输入!\n");
            break;
        }

    }
    
    return true;

    error:
    return false; 
}


STU_LINK Creat_Student_Inf_Node()
{
    STU_LINK new_stu_node = (STU_LINK)malloc(sizeof(STU_NODE));
    if (new_stu_node == (STU_LINK)NULL)
    {
        perror("malloc...");
        return (STU_LINK)NULL;
    }
    memset(new_stu_node,0,sizeof(STU_NODE));

    new_stu_node->next = new_stu_node;
    new_stu_node->prev = new_stu_node;

    printf("\n-----添加学生信息-----\n");
    printf("请输入学生的学号:");
    scanf("%u",&new_stu_node->stu_id);
    printf("请输入学生的姓名:");
    scanf("%s",new_stu_node->stu_name);
    printf("请输入学生的性别(男/女):");
    scanf("%s",new_stu_node->stu_sex);
    printf("请输入学生的成绩:");
    scanf("%u",&new_stu_node->stu_score);
    return new_stu_node;
}


bool Add_Student_Node(TEA_p teacher)
{
    STU_LINK new_stu_node =Creat_Student_Inf_Node();
    if (new_stu_node == (STU_LINK)NULL)
    {
        printf("创建新节点失败\n");
        return false;
    }
    
    if (teacher->head_node->next == teacher->head_node)
    {
        teacher->head_node->next = new_stu_node;
        teacher->head_node->prev = new_stu_node;

        new_stu_node->next = teacher->head_node;
        new_stu_node->prev = teacher->head_node;
    }
    else
    {
        //尾插法
        new_stu_node->prev = teacher->head_node->prev;
        teacher->head_node->prev->next =new_stu_node;

        new_stu_node->next = teacher->head_node;
        teacher->head_node->prev = new_stu_node;

    }
   printf("添加成功\n");

    return true;
}


bool Delete_Student(TEA_p teacher)
{
    if (teacher == (TEA_p)NULL)
    {
        printf("头节点异常\n");
        return false;
    }
    STU_LINK del_node = Search_Student(teacher);
    if (del_node == (STU_LINK)NULL)
    {
        printf("没有该学生，删除失败!\n");
        return true;
    }
    else
    {
     
     del_node->prev->next = del_node->next;   
     del_node->next->prev = del_node->prev;
     
     /* del_node->next = (STU_LINK)del_node;
     del_node->prev = (STU_LINK)del_node; */

    }
    free(del_node);
    printf("删除成功!\n");
    
    return true;
}


STU_LINK Search_Student(TEA_p teacher)
{
    if (teacher == (TEA_p)NULL)
    {
        printf("头节点异常\n");
        return false;
    }
    if (teacher->head_node->next == teacher->head_node)
    {
        printf("当前没有学生信息!\n");
        return (STU_LINK)NULL;
    }
    
    unsigned int obj_id;
    printf("请输入需要操作的学生学号：");
    scanf("%u",&obj_id);

    STU_LINK obj_node = teacher->head_node->next;
    for ( ; 
            obj_node != teacher->head_node ; 
            obj_node = obj_node->next)
    {
        if (obj_node->stu_id == obj_id)
        {
            printf("当前信息为\n");
            printf("\n%-10s %-20s %-10s %-10s\n", 
                "学号", "姓名", "成绩", "性别");
            printf("--------------------------------------------\n");
            printf("\n%-10u %-20s %-10u %-10s\n",
                obj_node->stu_id,
                obj_node->stu_name,
                obj_node->stu_score,
                obj_node->stu_sex);
                
                return obj_node;
        }
        
    }
    
    printf("查找失败，没有该学生!\n");

    return (STU_LINK)NULL;
}
   

bool Modify_Student(TEA_p teacher)
{
    if (teacher == (TEA_p)NULL)
    {
        printf("头节点异常\n");
        return false;
    }

    STU_LINK obj_node = Search_Student(teacher);
    if (obj_node == (STU_LINK)NULL)
    {
        printf("没有该学生,修改失败!\n");
        return true;
    }
    printf("\n-----修改学生信息-----\n");
    printf("请输入要修改的学号:");
    scanf("%u",&obj_node->stu_id);
    printf("请输入要修改的姓名:");
    scanf("%s",obj_node->stu_name);
    printf("请输入要修改的性别(男/女):");
    scanf("%s",obj_node->stu_sex);
    printf("请输入要修改的成绩:");
    scanf("%u",&obj_node->stu_score);


    return true;
}


bool Show_All_Student(TEA_p teacher)
{
    if (teacher == (TEA_p)NULL)
    {
        printf("头节点异常\n");
        return false;
    }
    
    if (teacher->head_node->next == teacher->head_node)
    {
       printf("当前没有学生信息\n");
       return true;
    }
    else
    {
        printf("\n%-10s %-10s %-10s %-10s\n", 
            "学号", "姓名", "成绩", "性别");
        printf("--------------------------------------------\n");
    for (STU_LINK tmp_node = teacher->head_node->next;
                tmp_node != teacher->head_node;
                tmp_node = tmp_node->next)
        {
            printf("\n%-10u %-10s %-10u %-10s\n",
                tmp_node->stu_id,
                tmp_node->stu_name,
                tmp_node->stu_score,
                tmp_node->stu_sex);
        }


    }
    
    
    return true;
}


bool Sort_Score(TEA_p teacher) {
    if (!teacher ||  teacher->head_node->next == teacher->head_node) {
        printf("无需排序\n");
        return false;
    }

    printf("按成绩排序之前学生信息\n");
    Show_All_Student(teacher);

    //冒泡排序
    STU_LINK head = teacher->head_node;
    STU_LINK current,next;
    int swap_count;
    for (STU_LINK i = head->next;
         i->next != head; i = i->next )
    {   
        current = head->next;
        next    = current->next;
        swap_count =0;
        for ( ; next != head; )
        {
            if (current->stu_score < next->stu_score)
            {
                current->prev->next = next;
                next->prev = current->prev;
                current->next= next->next;
                next->next->prev = current;
                current->prev = next;
                next->next  = current;

                //更新current和next
                STU_LINK temp = current;
                current       = next;
                next          = temp;

                //将交换次数值为一
                swap_count = 1;
            }
            //整体右移,继续去比较
            current = next;
            next    = next->next;
        }
        if (swap_count == 0)//说明本轮没有交换
        {
            break;
        }
    }
    
    printf("按成绩顺序排序之后学生信息\n");
    Show_All_Student(teacher);

    return true;
}


bool Free_System(TEA_p teacher)
{
    if (teacher == (TEA_p)NULL)
    {
        printf("头节点异常\n");
        return false;
    }

    while (teacher->head_node->next != teacher->head_node)
    {
        STU_LINK free_node = teacher->head_node->next;

        free_node->next->prev = free_node->prev;
        free_node->prev->next = free_node->next;

        free_node->next = (STU_LINK)NULL;
        free_node->prev = (STU_LINK)NULL;

        free(free_node);
        
    }
        free(teacher->head_node);
        free(teacher);
    return true;
}

